//
//  LoginViewController.m
//  Umoney
//
//  Created by zwz on 16/8/2.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    NSString *Login;
    NSString *UID;
    NSArray *res;
}

@end

@implementation LoginViewController

- (IBAction)DoLoginEvent:(id)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:UID forKey:@"uid"];
    
    res=NULL;
    Login = NULL;
    
    if ([self.uid.text isEqual:@""]||[self.Password.text isEqualToString:@""]) {
        [self altererror];
    }
    else{
        NSString *urlString = [NSString stringWithFormat:@"http://23.106.134.160/umoney/login.php?uid=%@&password=%@",self.uid.text,self.Password.text];
        
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
        config.timeoutIntervalForRequest = 30.0f;
        config.timeoutIntervalForResource = 60.0f;
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
        NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            res = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            for (NSDictionary *x in res) {
                Login = [x valueForKey:@"login"];
                UID = [x valueForKey:@"uid"];
                self.MoneyTotal = [x valueForKey:@"totalmoney"];
                self.MoneyBalance = [x valueForKey:@"balancemoney"];
                self.Username = [x valueForKey:@"username"];
            }
            
        }];
        [task resume];
        
        while (!res) {
            [NSThread sleepForTimeInterval:0.3];
        }
        
        if (res) {
            
            if (Login) {
                //测试用
                [_delegate SetInfomation];
                
                self.IsLogin = YES;
                
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:UID forKey:@"uid"];
                [self dismissViewControllerAnimated:NO completion:nil];
            }
            else
            {
                [self alter];
            }

        }
        
        [self.uid setText:@""];
        [self.Password setText:@""];

    }
}
- (IBAction)DoRegisterEvent:(id)sender
{
    RegisterViewController *registers = [RegisterViewController new];
    [self presentViewController:registers animated:NO completion:nil];
    
    //[self dismissViewControllerAnimated:NO completion:nil];
    
}
- (IBAction)DoAlterEvent:(id)sender
{
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self addGesture];
    // Do any additional setup after loading the view from its nib.
    //self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.title = @"登录";
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)alter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
}

-(void)altererror
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}

-(void)addGesture
{
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewtaped:)];
    tap1.cancelsTouchesInView=NO;
    [self.view addGestureRecognizer:tap1];
}


-(void)viewtaped:(UIGestureRecognizer *)tap1
{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
